<?php
		/**
	 * 根据标示符(uid或uname或email或domain)获取用户信息
	 * 
	 * 首先检查缓存(缓存ID: user_用户uid / user_用户uname), 然后查询数据库(并设置缓存).
	 * 
	 * @param string|int $identifier      标示符内容
	 * @param string     $identifier_type 标示符类型. (uid, uname, email, domain之一)
	 */

class UserModel extends Model {
	public function getUserByIdentifier($identifier, $identifier_type = 'uid'){
		if ($identifier_type == 'uid' && !is_numeric($identifier))
			return false;
		else if (!in_array($identifier_type, array('uid','uname','email','domain')))
			return false;
		else if (($identifier_type == 'uid' || $identifier_type == 'uname') && ($user = object_cache_get("user_{$identifier}")) !== false)
			return $user;
		
		$map = array();
//		$map['is_init']   = 1;
//		$map['is_active'] = 1;
		if ($identifier_type == 'uid')
			$map['uid'] = intval($identifier);
		else
			$map[$identifier_type] = t($identifier);
		
		if ($user = $this->where($map)->find()) {
			object_cache_set("user_{$user['uid']}",   $user);
			object_cache_set("user_{$user['uname']}", $user);
		} else if ($identifier_type == 'uid' || $identifier_type == 'uname') {
			object_cache_set("user_{$identifier}", array());
		}
		return $user;
	}
}
?>